package org.dingyom.car.service.impl;

import io.swagger.annotations.Authorization;
import org.dingyom.car.domain.Car;
import org.dingyom.car.domain.CarDetail;
import org.dingyom.car.mapper.CarDetailMapper;
import org.dingyom.car.mapper.CarMapper;
import org.dingyom.car.service.ICarDetailService;
import org.dingyom.basics.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingyom
 * @since 2022-09-08
 */
@Service
public class CarDetailServiceImpl extends BaseServiceImpl<CarDetail> implements ICarDetailService {
    @Autowired
    private CarDetailMapper carDetailMapper;
    @Autowired
    private CarMapper carMapper;
    @Override
    public void addOrUpdate(CarDetail carDetail) {
        CarDetail detail = carDetailMapper.loadByCarId(carDetail.getCarId());
        carDetail.setDetectiontime(new Date());
        if (detail != null) {//存在就是更新
            carDetailMapper.addOrUpdate(carDetail);
        }else {//不存在就是新增
            Car car = carMapper.loadById(carDetail.getCarId());
            carDetail.setCartitle(car.getTitle());
            carDetailMapper.save(carDetail);
        }
    }

    @Override
    public CarDetail getByCarId(Long carId) {
        return carDetailMapper.loadByCarId(carId);
    }
}
